#include "fft_if.h"
#include <malloc.h>
#include <gsl/gsl_fft_complex.h>


int numba_cruncha_fft(complex * Ew, size_t n){
	size_t nn = 2*n;
	if(nn <= n){
		return 1;//No upper limit
	}
		
	double * data = malloc(sizeof(double)*nn); // Malloc and copy - not so optimal, but not critical at the time of writing
	if(data==NULL){
		return 1;
	}
	
	size_t j;
	for(j=0;j<n;j++){
		data[2*j]=creal(Ew[j]);
		data[2*j+1]=cimag(Ew[j]);
	}

	gsl_fft_complex_wavetable * wavetable;
	gsl_fft_complex_workspace * workspace;
	wavetable = gsl_fft_complex_wavetable_alloc (n);
	workspace = gsl_fft_complex_workspace_alloc (n);

	gsl_fft_complex_forward(data,1,n,wavetable,workspace);

	gsl_fft_complex_wavetable_free (wavetable);
	gsl_fft_complex_workspace_free (workspace);
	for(j=0;j<n;j++){
		Ew[j]=data[2*j] + I*data[2*j+1];
	}
	
	free(data);
	return 0;
}
